
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="vocabulary">
            <div class="item">
              <span v-html="'词汇'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.vocabulary"
                placeholder="请输入词汇"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <div class="topBtn">
      <el-button
        v-has="'packageReview:efficacyWord:add'"
        size="mini"
        @click="addEffectClick"
        type="success"
        plain
        icon="el-icon-plus"
        >新增功效</el-button
      >
    </div>

    <div class="tableBox" ref="tableBoxRef">
      <el-table
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        :height="screenHeight - 400"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="dict"
          align="center"
          label="功效类别"
           
        >
        <template slot-scope="scope">
          {{dictMap[scope.row.dict]}}
        </template>
        </el-table-column>

        <el-table-column
          prop="vocabulary"
          align="center"
          label="涉及词汇"
           
        >
        </el-table-column>
        <el-table-column
          prop="comment"
          align="center"
          label="功效评价"
           
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'packageReview:efficacyWord:edit'"
              @click="editClick(scope.row, scope.$index)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="delClick(scope.row)"
              v-has="'packageReview:efficacyWord:DELETE'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="dict" label="功效类别：">
            <div class="item">
              <el-select filterable 
                clearable
                size="mini"
                v-model="addEditForm.dict"
                placeholder="请选择功效类别"
              >
                <el-option
                  v-for="item in typeOptions"
                  :key="item.v"
                  :label="item.v"
                  :value="item.k"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>

          <el-form-item prop="vocabulary" label="涉及词汇：">
            <div class="item">
              <el-input
                size="mini"
                type="textarea"
                v-model="addEditForm.vocabulary"
                placeholder="如需添加多个，请用英文逗号隔开，最后一个词后面不可加符号。如：aaa,bbb,ccc,ddd"
                :rows="6"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="comment" label="功效评价：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.comment"
                placeholder="请输入功效评价"
              >
              </el-input>
            </div>
          </el-form-item>

          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      screenHeight: document.documentElement.clientHeight,
      typeOptions: [],
      searchForm: {
        vocabulary: "",
      },
      tableLoading: false,
      tableHeight: 50,
      tableData: [],
      addEditDialogVisible: false,
      addEditForm: {
        dictId: "",
        vocabulary: "",
        comment: "",
        dict:""
      },
      addEditRules: {
        dictId: [{ required: true, message: "请选择功效类别", trigger: "blur" }],
        vocabulary: [
          { required: true, message: "请输入涉及词汇", trigger: "blur" },
        ],
        comment: [
          { required: true, message: "请输入功效评价", trigger: "blur" },
        ],
      },
      title: "新增功效",
      editId: "",
    };
  },
  computed: {
    ...mapGetters(["dictList"]),
      dictMap() {
      let map = {}
      this.typeOptions.forEach(x => {
        map[x.k] = x.v
      })
      return map
    },
  },

  created() {
    this.getEffectList();
  },
  methods: {
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.getEffectList();
    },
    searchClick() {
      this.getEffectList();
    },



    /**
     * 获取标准库列表
     * @param {*}
     */
    async getEffectList() {
      this.tableLoading = true;
      const res = await this.$api.effectList({
        ...this.searchForm,
      });
      this.tableData = res.data.result;
      this.typeOptions = this.dictList.effect_type;
      this.tableLoading = false;
    },

    /**
     * 新增功效
     * @param {*}
     */
    addEffectClick() {
      this.title = "新增功效";
      this.addEditDialogVisible = true;
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row, index) {
      this.title = "编辑功效";
      this.addEditDialogVisible = true;
      this.editId = row.id;

      this.$nextTick(() => {
        this.addEditForm = JSON.parse(JSON.stringify(this.tableData[index]));
      });
    },
    /**
     * 删除按钮
     * @param {*} row
     */
    delClick(row) {
      this.$confirm(
        "此操作不可恢复，确认继续删除吗?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$api.delEffect(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getEffectList();
            }
          });
        })
        .catch(() => {});
    },

    /**
     * 打开新增和修改的回调
     * @param {*}
     */
    addEditDialogOpen() {
      if (this.$refs.addEditFormRef) this.$refs.addEditFormRef.resetFields();
    },

    /**
     * 新增编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
           this.addEditForm.dictId=this.addEditForm.dict
          let data = {
            id: this.editId,
            ...this.addEditForm,
          };
          if (this.title == "新增功效") {
            data.id = "";
            const res = await this.$api.addEffect(data);
            if (res.data.code == "00000") {
              this.$message.success("新增成功");
              this.getEffectList();
              this.addEditDialogVisible = false;
            }
          } else {
            const res = await this.$api.editEffect(data);
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
              this.getEffectList();
              this.addEditDialogVisible = false;
            }
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
   .topBtn {
    margin: 10px 0;
text-align: right;
  }
  .tableBox {
    flex: 1;
    .bottomBtn {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>